package com.auxgroup.smarthome.inner.repo;

import com.auxgroup.bridge.app.inner.vo.DeviceFaultVo;
import com.auxgroup.bridge.app.inner.vo.ShareUserListVo;
import com.auxgroup.bridge.app.inner.vo.SubShareUserVo;
import com.auxgroup.smarthome.inner.entity.UserDeviceEntity;
import com.auxgroup.smarthome.repo.GenericDao;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 用户设备关系
 */
public interface UserDeviceEntityRepo extends GenericDao<UserDeviceEntity,String> {

    List<UserDeviceEntity> findByUidAndTrashFalse(String uid);

    List<UserDeviceEntity> findByDeviceIdAndTrashFalse(String deviceId);

    UserDeviceEntity findByDeviceIdAndUidAndTrashFalse(String deviceId, String uid);

    UserDeviceEntity findByIdAndTrashFalse(String shareId);

    UserDeviceEntity findByUidAndDeviceIdAndTrashFalse(String userId, String deviceId);

    UserDeviceEntity findByUidAndDeviceIdAndUserTagAndTrashFalse(String userId, String deviceId, int userTag);

    @Query(value = "select count(*) from user_device where uid = ? and device_id = ? and is_trash = 0",nativeQuery = true)
    Integer countByUidMappedDeviceId(String uid, String deviceId);

    @Query(value = "select * from user_device where uid = ? and device_id = ? and is_trash = 0",nativeQuery = true)
    UserDeviceEntity findByUidAndDeviceId(String uid, String deviceId);

    /**
     * 查询设备是否已经有主人绑定
     * @param deviceId
     * @return
     */
    @Query(value = "select count(*) from user_device where device_id = ? and is_trash = 0 and user_tag = 0",nativeQuery = true)
    Integer countByDeviceId(String deviceId);

    /**
     * 查询设备和主人的关系
     * @param deviceId
     * @param userTag 0:主人
     * @return
     */
    UserDeviceEntity findByDeviceIdAndUserTagAndTrashFalse(String deviceId,Integer userTag);

    /**
     * 逻辑删除某个设备的所有用户关系
     * @param deviceId
     * @return
     */
    @Query(value = "update user_device set is_trash=1 where device_id = ?" ,nativeQuery = true)
    @Modifying
    @Transactional
    Integer deleteRelationship(String deviceId);

    @Query("select new com.auxgroup.bridge.app.inner.vo.ShareUserListVo(ds.id, ds.uid, ds.username, ds.deviceId, ds.userTag, ds.expiredAt) from UserDeviceEntity ds where ds.trash=0 and ds.shareUid = ?1")
    List<ShareUserListVo> findByShareUserId(String shareUid);

    /**
     * 根据分享者uid和设备id获取分享用户列表
     * @param shareUid
     * @param deviceId
     * @return
     */
    @Query("select new com.auxgroup.bridge.app.inner.vo.ShareUserListVo(ds.id, ds.uid, ds.username, ds.deviceId, ds.userTag, ds.expiredAt) from UserDeviceEntity ds where ds.trash=0 and ds.shareUid = ?1 and ds.deviceId=?2")
    List<ShareUserListVo> findByShareUserIdAndDeviceId(String shareUid, String deviceId);

    @Query("select new com.auxgroup.bridge.app.inner.vo.SubShareUserVo(ds.uid, ds.username, ds.userTag) from UserDeviceEntity ds where ds.trash=0 and ds.shareUid = ?1 ")
    List<SubShareUserVo> findSubuserByShareUid(String shareUid);

    /**
     * modify by lixiaoxiao 20170810 新增根据mac获取绑定主人的用户名和手机号
     */
    @Query("select new com.auxgroup.bridge.app.inner.vo.DeviceFaultVo(ds.username,ds.phone,ds.mac) from UserDeviceEntity ds where ds.trash=0 and ds.userTag = 0 and ds.mac = ?1 ")
    DeviceFaultVo findByMac(String mac);

    /**
     * 查找父分享者 分享给 子分享者的设备列表
     * @param pUid
     * @param uid
     * @param userTag
     * @return
     */
    List<UserDeviceEntity> findByShareUidAndUidAndUserTagAndTrashFalse(String pUid, String uid, Integer userTag);

    /**
     * 查找父分享者 分享给 子分享者的设备列表 -----用于获取好友分享设备列表
     * @param pUid
     * @param uid
     * @param userTag
     * @param qrBatch
     * @return
     */
    List<UserDeviceEntity> findByShareUidAndUidAndUserTagAndQrBatchAndTrashFalse(String pUid, String uid, Integer userTag, String qrBatch);

    /**
     *  查找
     * @param pUid
     * @param uid
     * @param deviceId
     * @return
     */
    List<UserDeviceEntity> findByShareUidAndUidAndDeviceIdAndTrashFalse(String pUid, String uid, String deviceId);


}
